package com.util.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author xin.ding
 * @date 2020/10/31 9:51
 */
public class CallableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<String> callable = () -> "this is callable";

//        Callable<String> callable = new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                return null;
//            }
//        };



        FutureTask<String> task = new FutureTask<>(callable);

        Thread t = new Thread(task);
        t.start(); // 启动线程
        System.out.println(task.isDone());
        String s = task.get();
        System.out.println(s);
        System.out.println(task.isDone());

        task.cancel(true); // 取消线程

    }
}
